//app.js
App({
  onLaunch: function () {
    var that = this;
    //
    wx.login({
      success: login => {
        wx.getUserInfo({
          complete: function () {
            wx.getWeRunData({
              complete: function () {
                that.isAuthorize();
              }
            })
          }
        })
      }
    })
  },
  /**
   * 打开授权设置
   */
  reOpenSetting: function () {
    wx.openSetting({
      success: (res) => {
        res.authSetting = {
          "scope.userInfo": true,
          "scope.werun": true
        }
        this.isAuthorize()
      }
    })
  },
  /**
   * 是否全部授权
   */
  isAuthorize: function () {
    var that = this;
    wx.getSetting({
      success(res) {
        // 判断是否有用户信息授权
        if (!res.authSetting['scope.userInfo']) {
          that.authorizeUserInfo();
          return;
        }
        // 判断是否有微信运动授权
        if (!res.authSetting['scope.werun']) {
          that.authorizeWeRun();
          return;
        }

        if (res.authSetting['scope.userInfo'] && res.authSetting['scope.werun']) {
          that.getHomeData();
        } else {
          that.isAuthorize();
        }
      }
    })
  },
  /**
   * 是否授权用户数据
   */
  authorizeUserInfo: function () {
    var that = this;
    wx.authorize({
      scope: 'scope.userInfo',
      success() {
        console.log('已授权【用户信息】')
      },
      fail() {
        wx.showModal({
          title: '提示',
          content: '必须授权【用户信息】之后才能操作，是否重新授权登陆？',
          success: function (res) {
            if (res.confirm) {
              that.reOpenSetting()
            } else if (res.cancel) {
              that.isAuthorize();
            }
          }
        })
      }
    })
  },
  /**
   * 是否授权微信运动
   */
  authorizeWeRun: function () {
    var that = this;
    wx.authorize({
      scope: 'scope.werun',
      success() {
        console.log('已授权【微信运动】')
      },
      fail() {
        wx.showModal({
          title: '提示',
          content: '必须授权【微信运动】之后才能操作，是否重新授权登陆？',
          success: function (res) {
            if (res.confirm) {
              that.reOpenSetting()
            } else if (res.cancel) {
              that.isAuthorize();
            }
          }
        })
      }
    })
  },
  /**
   * 获取首页数据
   */
  getHomeData: function () {
    var that = this;
    wx.login({
      success: login => {
        // 获取用户信息
        wx.getUserInfo({
          success: user => {
            // 获取运动数据
            wx.getWeRunData({
              success: res => {
                // 发送运动数据到服务器
                wx.request({
                  url: that.globalData.API_URL + 'home',
                  data: {
                    'code': login.code,
                    'encryptedData': res.encryptedData,
                    'iv': res.iv,
                    'nickName': user.userInfo.nickName,
                    'avatarUrl': user.userInfo.avatarUrl,
                    'province': user.userInfo.province,
                    'city': user.userInfo.city,
                    'country': user.userInfo.country,
                    'language': user.userInfo.language,
                    'gender': user.userInfo.gender,
                  },
                  header: {
                    'content-type': 'application/x-www-form-urlencoded'
                  },
                  method: 'post',
                  success: function (r) {
                    if (r.data.code == 0) {
                      that.globalData.homeData = r.data.data
                      that.globalData.user = r.data.data.user
                      that.globalData.token = r.data.data.user.token
                      that.globalData.indexDataHaveChange = true;
                      that.globalData.myDataHaveChange = true;
                      that.globalData.appGetDataFinish = true;
                    } else {
                      wx.showToast({
                        title: '登陆失败，请联系管理员',
                        image: '/images/toast_icon_warn.png',
                        duration: 5000
                      })
                    }
                  }
                })
              }
            })
            that.globalData.userInfo = user.userInfo;
          }
        })
      }
    })
  },
  globalData: {
    // API_URL: 'https://go.x-i-tech.com/api/',
    API_URL: 'http://192.168.1.159:7000/api/',
    userInfo: null,
    homeData: null,
    user: null,
    token: null,
    indexDataHaveChange: false,
    myDataHaveChange: false,
    appGetDataFinish: false,
  }
})